package com.xiangban.test_xiangban.service;

import com.xiangban.test_xiangban.domain.Parameter;
import com.xiangban.test_xiangban.domain.Test;
import com.xiangban.test_xiangban.domain.vo.ParameterVo;
import com.xiangban.test_xiangban.mapper.TestMapper;
import com.xiangban.test_xiangban.utils.JsonData;
import com.xiangban.test_xiangban.utils.WordUtil;
import freemarker.template.TemplateException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.logging.Handler;


@Service
public class TestService {
    @Value("${path.url}")
    private String path;
    @Value("${path.baseUrl}")
    private String baseUrl;
    @Value("${tempFile}")
    private String tempFile;
    @Autowired
    private TestMapper testMapper;

    public JsonData exportTest(ParameterVo parameterVo) throws IOException, TemplateException, BiffException {
        List<String> strings = getUser(parameterVo.getFileName());
        List<Test> tests = testMapper.findSubjectById(parameterVo);
//        if (tests.size()<parameterVo.getTestNum()){
//            return JsonData.buildError("测试数量超出题库总量");
//        }
//        if (parameterVo.getTestNum()==null || parameterVo.getTestNum()==0){
//            return JsonData.buildError("测试题数量不能为空或0");
//        }
//        Collections.shuffle(tests);
//        List<Test> tests1 = tests.subList(0, 2);
        HashMap<String, Object> map = new HashMap<>();
        LinkedList<Parameter> parameters = new LinkedList<>();
        int j = 0;
        for (int i = 0; i < strings.size(); i++) {
            Parameter parameter = new Parameter();
            BeanUtils.copyProperties(parameterVo, parameter);
            parameter.setName(strings.get(i));

            parameter.setTitle(tests.get(j).getTitle());
            parameter.setShowCase(tests.get(j).getShowCase());

            parameter.setTitle(tests.get(j).getTitle());
            parameter.setShowCase(tests.get(j).getShowCase());
            parameters.add(parameter);
            j++;
            j = j > 1 ? 0 : j;
        }
        map.put("ls", parameters);
        return JsonData.buildSuccess(baseUrl + WordUtil.testPaperWord(map, path, tempFile, UUID.randomUUID().toString().replace("-","") + ".docx"));


    }
    private List<String> getUser(String fileName) throws IOException, BiffException {
        //创建workbook
        Workbook workbook = Workbook.getWorkbook(new File(path+fileName));
        //获取第一个工作表sheet
        Sheet sheet = workbook.getSheet(0);
        LinkedList<String> linkedList = new LinkedList<>();

        //获取数据
        for (int i = 4; i < sheet.getRows(); i++) {
            for (int j = 1; j < sheet.getColumns(); j++) {
                if( j == 1 ){
                    Cell cell = sheet.getCell(j, i);
                    String s = cell.getContents() + sheet.getCell(7, i).getContents();
                    if (!"".equals(s)){
                        linkedList.add(s);
                    }
                }
            }
            System.out.println();
        }
        workbook.close();
        return linkedList;
    }
    //导入文件
    public String importExcel(MultipartFile file) {
        String fileName = UUID.randomUUID().toString().replace("-","")+".xls";//随机取一个名字
        String filePath = path;//文件地址
        File dest = new File(filePath + fileName);//文件
        try {
                file.transferTo(dest);

            return fileName;
        } catch (IOException e) {
        }
        return "";
    }

    public List<Test> getSubject(){
        List<Test> subject = testMapper.getSubject();
        return subject;

    }
}
